package org.weather.data.utils;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;

/**
 * Created by syf on 2018/6/20.
 */
/**
 * 保证序列化json时，如果是null对象，key也会消失
 * @param <T>
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ServiceResponse<T> implements Serializable{

    private int code;//状态码
    private String message;//提示
    private T data;//数据

    public ServiceResponse(int code){
        this.code = code;
    }

    public ServiceResponse(int code,T data){
        this.code = code;
        this.data = data;

    }

    public ServiceResponse(int code,T data,String message){
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public ServiceResponse(int code,String message){
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> ServiceResponse<T> createBySuccess(){

        return new ServiceResponse<T>(RespondCode.SUCCESS.getCode());
    }

    public static <T> ServiceResponse<T> createBySuccessMessage(String msg){

        return new ServiceResponse<T>(RespondCode.SUCCESS.getCode(),msg);
    }

    public static <T> ServiceResponse<T> createBySuccess(T data){

        return new ServiceResponse<T>(RespondCode.SUCCESS.getCode(),data);
    }

    public static <T> ServiceResponse<T> createBySuccess(String msg,T data){

        return new ServiceResponse<T>(RespondCode.SUCCESS.getCode(),data,msg);
    }

    public static <T> ServiceResponse<T> createByError(){

        return new ServiceResponse<T>(RespondCode.ERROR.getCode(),RespondCode.ERROR.getDesc());
    }

    public static <T> ServiceResponse<T> createByErrorMessage(String msg){

        return new ServiceResponse<T>(RespondCode.ERROR.getCode(),msg);
    }

    public static <T> ServiceResponse<T> createByErrorCodeMessage(int errorode,String errorMessage){

        return new ServiceResponse<T>(errorode,errorMessage);
    }
}
